package org.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.example.domain.FruitInbound;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface FruitInboundMapper extends BaseMapper<FruitInbound> {

    /**
     * 根据条件查询入库单列表
     */
    @Select({
            "<script>",
            "SELECT * FROM fruit_inbound",
            "<where>",
            "  <if test='fruitName != null and fruitName != \"\"'>",
            "    AND fruit_name LIKE CONCAT('%', #{fruitName}, '%')",
            "  </if>",
            "  <if test='supplierName != null and supplierName != \"\"'>",
            "    AND supplier_name LIKE CONCAT('%', #{supplierName}, '%')",
            "  </if>",
            "  <if test='startTime != null and startTime != \"\"'>",
            "    AND create_time >= #{startTime}",
            "  </if>",
            "  <if test='endTime != null and endTime != \"\"'>",
            "    AND create_time &lt;= #{endTime}",
            "  </if>",
            "</where>",
            "ORDER BY create_time DESC",
            "LIMIT #{offset}, #{pageSize}",
            "</script>"
    })
    List<FruitInbound> selectInboundList(
            @Param("fruitName") String fruitName,
            @Param("supplierName") String supplierName,
            @Param("startTime") String startTime,
            @Param("endTime") String endTime,
            @Param("offset") Integer offset,
            @Param("pageSize") Integer pageSize
    );

    /**
     * 根据条件查询入库单总数
     */
    @Select({
            "<script>",
            "SELECT COUNT(*) FROM fruit_inbound",
            "<where>",
            "  <if test='fruitName != null and fruitName != \"\"'>",
            "    AND fruit_name LIKE CONCAT('%', #{fruitName}, '%')",
            "  </if>",
            "  <if test='supplierName != null and supplierName != \"\"'>",
            "    AND supplier_name LIKE CONCAT('%', #{supplierName}, '%')",
            "  </if>",
            "  <if test='startTime != null and startTime != \"\"'>",
            "    AND create_time >= #{startTime}",
            "  </if>",
            "  <if test='endTime != null and endTime != \"\"'>",
            "    AND create_time &lt;= #{endTime}",
            "  </if>",
            "</where>",
            "</script>"
    })
    Integer selectInboundCount(
            @Param("fruitName") String fruitName,
            @Param("supplierName") String supplierName,
            @Param("startTime") String startTime,
            @Param("endTime") String endTime
    );

    /**
     * 根据ID查询入库单
     */
    @Select("SELECT * FROM fruit_inbound WHERE id = #{id}")
    FruitInbound selectById(@Param("id") Long id);

    /**
     * 删除入库单
     */
    @Select("DELETE FROM fruit_inbound WHERE id = #{id}")
    void deleteById(@Param("id") Long id);
    /***
     * 拒绝入库
     * @param id
     */
    @Update("update fruit_inbound set status=#{status} where id=#{id}")
    void changeStatusById(@Param("id") Long id, @Param("status") String status);
}